package com.cty._01_Base._09_02_StackWithTwoQueues;

import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;

/**
 * @Auther: cty
 * @Date: 2020/7/1 9:48
 * @Description: 面试题9相关题目：用两个队列实现一个栈
 * @version: 1.0
 */
public class StackWithTwoQueues {
    private Queue<Character> queue1;
    private Queue<Character> queue2;

    public StackWithTwoQueues(int capacity){
        queue1 = new ArrayBlockingQueue<Character>(capacity);
        queue2 = new ArrayBlockingQueue<Character>(capacity);
    }

    public boolean isEmpty(){
        return queue1.isEmpty() && queue2.isEmpty();
    }

    public void push(char c){
        if(!queue2.isEmpty())
            queue2.add(c);
        else
            queue1.add(c);
    }  // end push()

    public char pop(){
        while(!queue1.isEmpty()){
            if(queue1.size() == 1)
                return queue1.remove();
            queue2.add(queue1.remove());
        }

        while(!queue2.isEmpty()){
            if(queue2.size() == 1)
                return queue2.remove();
            queue1.add(queue2.remove());
        }

        return '#';
    }  // end pop()

}  // end class
